layui.use(['form', 'table', 'layer'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layer = layui.layer;


    table.render({
        id: 'currentTableId',
        elem: '#currentTableId',
        url: '/api/order/',
        request: {
            pageName: 'current' //页码的参数名称，默认：page
            , limitName: 'size' //每页数据量的参数名，默认：limit
        },
        cols: [[
            {field: 'rid', title: '订单编号', sort: true},
            {field: 'cname', title: '客户姓名'},
            {field: 'etruename', title: '下单员工'},
            {field: 'rtime', title: '下单时间'},
            {
                field: 'ris', title: '订单状态', minWidth: 150, templet: (data) => {
                    if (0 === data.ris) {
                        return "已废弃";
                    } else if (1 === data.ris) {
                        return "未完成";
                    } else if (2 === data.ris) {
                        return "已完成";
                    }
                }
            },
            {title: '订单详情', minWidth: 50, templet: '#currentTableBar', fixed: "right", align: "center"}
        ]],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });


    // 重载表单
    let reloadTable = (param) => {
        table.reload('currentTableId', {
            url: '/api/order/'
            , where: {
                ris: param.ris,
                pid: param.pid,
                eid: param.eid,
                cid: param.cid
            }
        });
    };


    $(() => {
        //初始化部门名称数据
        $.get({
            url: "/api/department/partList", success: (resp) => {
                resp.forEach(r => {
                    $("#part").append("<option value='" + r.pid + "'>" + r.pname + "</option>");
                });
                form.render('select');
            }, dataType: "json"
        });
    });


    //部门名称select监听器
    form.on('select(partFilter)', (data) => {
        //部门id
        let pid = $('#part option:selected').val();
        if (!!pid) {
            $('.e-class').remove();
            $('.c-class').remove();
            $.get({
                url: "/api/emp/" + pid + "", success: (resp) => {
                    resp.forEach(r => {
                        $("#emp").append("<option class='e-class' value='" + r.eid + "'>" + r.etruename + "</option>");
                    });
                    form.render('select');
                }, dataType: "json"
            });
            reloadTable({'pid': pid, 'eid': '', 'cid': ''});
        }
    });

    //员工名称select监听器
    form.on('select(empFilter)', (data) => {
        //员工id
        let eid = $('#emp option:selected').val();
        let pid = $('#part option:selected').val();
        //根据员工id查询客户列表
        if (!!eid) {
            $('.c-class').remove();
            $.get({
                url: "/api/Client/" + eid, success: (resp) => {
                    resp.forEach(r => {
                        $("#client").append("<option class='c-class' value='" + r.cid + "'>" + r.cname + "</option>");
                    });
                    form.render('select');
                }, dataType: "json"
            });
            reloadTable({'pid': pid, 'eid': eid, 'cid': ''});
        }
    });


    form.on('select(clientFilter)', (data) => {
        //员工id
        let cid = $('#client option:selected').val();
        let eid = $('#emp option:selected').val();
        let pid = $('#part option:selected').val();
        //根据乘客和id查询客户列表
        reloadTable({'pid': pid, 'eid': eid, 'cid': cid});
        form.render('select');
    });


    //查询废弃订单
    $("#order-btn3").on("click", () => {
        reloadTable({'ris': 0})
    });

    // 查询未完成订单
    $("#order-btn1").on("click", () => {
        reloadTable({'ris': 1})
    });

    // 查询已完成订单
    $("#order-btn2").on("click", () => {
        reloadTable({'ris': 2})
    });

    // 添加订单
    $("#order-add-btn").on("click", function () {
        layer.open({
            type: 2 //此处以iframe举例
            , title: '新增订单'
            , area: ['400px', '400px']
            , shade: 0
            , maxmin: true
            , content: '/page/addOrder.html'
            , btn: ['提交', '关闭']
            , yes: function (index) {

                var data = window['layui-layer-iframe' + index].getValue();

                //console.log(data);

                if (!data.eid) {
                    layer.msg("员工名称不能为空");
                    return false;
                }
                if (!data.cid) {
                    layer.msg("客户名称不能为空");
                    return false;
                }

                //发送新增请求 get/post
                $.post({
                    url: "/api/order/",
                    data: data,
                    success: (resp) => {
                        //  console.log(resp);

                        //post请求执行成功的回调
                        if (resp.code === 1) {
                            layer.close(index);
                            //刷新表格
                            reloadTable({});
                            layer.msg("新增成功");
                        } else {
                            layer.msg(response.data.msg)
                        }
                    }, dataType: "json"
                });

            }
            , btn2: function (index) {//5
                layer.close(index);
            }
        });
    });


    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'desc') {
            // console.log('订单id', data.rid)
            //跳转到订单详情页面
            location.href = '/page/order-details.html?rid=' + data.rid;
        }
    });


});